package Presenter;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import Models.RecipeSalts;
import Models.SelectedSalt;

public class RecipeSaltsPresenter extends AbstractPresenter<RecipeSalts> {
	final String SINGLEITEM = "recipeSalts";
	final String MULTIITEM = "recipeSaltss";
	
	public ArrayList<RecipeSalts> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public RecipeSalts tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public RecipeSalts tryJSONParseNoHeader(String input){
		try{
			JSONObject jsonHop = new JSONObject(input);
			RecipeSalts item = createItemFromJSONObject(jsonHop);
			return item;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public RecipeSalts createItemFromJSONObject(JSONObject jsonObject){
		RecipeSalts item = new RecipeSalts();
		try{
			item.setId(jsonObject.getInt("recipeSaltID"));
			item.setDescription(jsonObject.getString("recipeSaltDesc"));
			
			if(!jsonObject.isNull("profileID")){
				//create product if set
				StyleWaterProfilesPresenter presenter = new StyleWaterProfilesPresenter();
				JSONObject jsonGroup = jsonObject.getJSONObject("profileID");
				item.setWaterProfile(presenter.createItemFromJSONObject(jsonGroup));
			} else {
				item.setWaterProfile(null);
			}
			
			//Set MashStep Collection
			ArrayList<SelectedSalt> selectedSalts = new ArrayList<SelectedSalt>();
			if(!jsonObject.isNull("selectedSaltCollection")){
				Object itm = jsonObject.get("selectedSaltCollection");
				SelectedSaltPresenter presenter = new SelectedSaltPresenter();
				if(itm instanceof JSONArray){
					JSONArray jPSTArray = (JSONArray)itm;
					for(int i=0; i<jPSTArray.length(); i++){
						JSONObject jsonItem = jPSTArray.getJSONObject(i);
						selectedSalts.add(presenter.createItemFromJSONObject(jsonItem));
					}
				} else {
					JSONObject jsonItem = (JSONObject)itm;
					selectedSalts.add(presenter.createItemFromJSONObject(jsonItem));
				}
				item.setSelectedSalts(selectedSalts);
			}
			
			return item;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(RecipeSalts item){
		try{
			JSONObject json = new JSONObject();
			json.put("recipeSaltID", item.getId());
			json.put("recipeSaltDesc", item.getDescription());

			if(item.getWaterProfile()!=null){
				StyleWaterProfilesPresenter presenter = new StyleWaterProfilesPresenter();
				json.put("profileID", presenter.createJSON(item.getWaterProfile()));
			} else {
				json.put("profileID", JSONObject.NULL);
			}
			
			//Create MashStep Collection
			if(item.getSelectedSalts()!=null){
				SelectedSaltPresenter presenter = new SelectedSaltPresenter();
				if(item.getSelectedSalts().size()>0){
					JSONArray jsonArray = new JSONArray();
					for(int i=0; i<item.getSelectedSalts().size(); i++){
						jsonArray.put(presenter.createJSON(item.getSelectedSalts().get(i)));
					}
					json.put("selectedSaltCollection", jsonArray);
				}
			}
			
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public RecipeSalts getEmptyEntity(){
		RecipeSalts entity = new RecipeSalts();
		return entity;
	}
}
